﻿using System.ComponentModel;

namespace Koala.Pro.CodeGenerator.UI.Controls
{
    public class SmoothProgressBar : UserControl
    {
        private Container components;

        private int min;

        private int max = 100;

        private int val;

        private Color BarColor = Color.Blue;

        public int Minimum
        {
            get
            {
                return min;
            }
            set
            {
                if (value < 0)
                {
                    min = 0;
                }
                if (value > max)
                {
                    min = value;
                    min = value;
                }
                if (val < min)
                {
                    val = min;
                }
                Invalidate();
            }
        }

        public int Maximum
        {
            get
            {
                return max;
            }
            set
            {
                if (value < min)
                {
                    min = value;
                }
                max = value;
                if (val > max)
                {
                    val = max;
                }
                Invalidate();
            }
        }

        public int Value
        {
            get
            {
                return val;
            }
            set
            {
                int num = val;
                if (value < min)
                {
                    val = min;
                }
                else if (value > max)
                {
                    val = max;
                }
                else
                {
                    val = value;
                }
                Rectangle clientRectangle = ClientRectangle;
                Rectangle clientRectangle2 = ClientRectangle;
                float num2 = (val - min) / (float)(max - min);
                clientRectangle.Width = (int)(clientRectangle.Width * num2);
                num2 = (num - min) / (float)(max - min);
                clientRectangle2.Width = (int)(clientRectangle2.Width * num2);
                Rectangle rc = default;
                if (clientRectangle.Width > clientRectangle2.Width)
                {
                    rc.X = clientRectangle2.Size.Width;
                    rc.Width = clientRectangle.Width - clientRectangle2.Width;
                }
                else
                {
                    rc.X = clientRectangle.Size.Width;
                    rc.Width = clientRectangle2.Width - clientRectangle.Width;
                }
                rc.Height = Height;
                Invalidate(rc);
            }
        }

        public Color ProgressBarColor
        {
            get
            {
                return BarColor;
            }
            set
            {
                BarColor = value;
                Invalidate();
            }
        }

        public SmoothProgressBar()
        {
            InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && components != null)
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            Name = "SmoothProgressBar";
            Size = new Size(384, 24);
        }

        protected override void OnResize(EventArgs e)
        {
            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            SolidBrush solidBrush = new SolidBrush(BarColor);
            float num = (val - min) / (float)(max - min);
            Rectangle clientRectangle = ClientRectangle;
            clientRectangle.Width = (int)(clientRectangle.Width * num);
            graphics.FillRectangle(solidBrush, clientRectangle);
            Draw3DBorder(graphics);
            solidBrush.Dispose();
            graphics.Dispose();
        }

        public void Increment(int value)
        {
            val += value;
        }

        private void Draw3DBorder(Graphics g)
        {
            int num = (int)Pens.White.Width;
            g.DrawLine(Pens.DarkGray, new Point(ClientRectangle.Left, ClientRectangle.Top), new Point(ClientRectangle.Width - num, ClientRectangle.Top));
            g.DrawLine(Pens.DarkGray, new Point(ClientRectangle.Left, ClientRectangle.Top), new Point(ClientRectangle.Left, ClientRectangle.Height - num));
            g.DrawLine(Pens.White, new Point(ClientRectangle.Left, ClientRectangle.Height - num), new Point(ClientRectangle.Width - num, ClientRectangle.Height - num));
            g.DrawLine(Pens.White, new Point(ClientRectangle.Width - num, ClientRectangle.Top), new Point(ClientRectangle.Width - num, ClientRectangle.Height - num));
        }
    }

}
